package com.isunland.managesystem.utils;

import android.support.annotation.NonNull;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.ExcManContent;
import com.isunland.managesystem.entity.PlanDetail;
import com.isunland.managesystem.entity.RSealsUseApp;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/2/16 10:41
 */

@RunWith(AndroidJUnit4.class) @LargeTest public class TestAndroidUtil {
  @Rule public ActivityTestRule<BaseVolleyActivity> mActivity =
      new ActivityTestRule<>(BaseVolleyActivity.class);
  //private MyUtils myUtil;

  public void setUp() throws Exception {
    //myUtil = new MyUtils();
  }

  //@org.junit.Test public void isJsonCorrectTest() throws Exception {
  //  assertEquals(false, MyUtils.isJsonCorrect(""));
  //  assertEquals(false, MyUtils.isJsonCorrect(null));
  //  assertEquals(true, MyUtils.isJsonCorrect("{}"));
  //}

  //@org.junit.Test public void testTitle() throws Exception {
  //
  //  assertEquals("123", calTitle("123"));
  //  assertEquals("国欣ERP+", calTitle(null));
  //  assertEquals("国欣ERP+", calTitle(""));
  //}

  @org.junit.Test public void testGsonDate() throws Exception {
    //String time0="{ \"result\": 1,\"message\":\"\",\"cause\":\"\",\"rows\":[{\"regDate\":\"2016-02-16\"}]}";
    //String time1="{ \"result\": 1,\"message\":\"\",\"cause\":\"\",\"rows\":[{\"regDate\":\"2016-02-16 11:11\"}]}";
    //String time2="{ \"result\": 1,\"message\":\"\",\"cause\":\"\",\"rows\":[{\"regDate\":\"2016-02-16 11:11:11\"}]}";
    //        assertEquals("2016-02-16", calDate(time0));
    //        assertEquals("2016-02-16", calDate(time1));
    //        assertEquals("2016-02-16", calDate(time2));
    //        SingleFragmentActivity.newInstance(new Fragment(),0,new BaseParams(),LoginActivity.class);
  }

  //private String calDate(String json){
  //    LeaseMainListOriginal result = new GsonBuilder()
  //            .setDateFormat(MyDateUtil.TYPE_DATE).create().fromJson(json, LeaseMainListOriginal.class);
  //   Date date=result.getRows().get(0).getRegDate();
  //     return MyDateUtil.parseDateToString(date);
  //    }
  private String calTitle(String realName) {
    return TextUtils.isEmpty(realName) ? mActivity.getActivity().getString(R.string.app_name)
        : realName;
  }

  @org.junit.Test public void testString() throws Exception {
    ArrayList<PlanDetail> mtaskList = new ArrayList<>();
    PlanDetail detail2 = new PlanDetail();
    detail2.setExcManName("2");
    PlanDetail detail3 = new PlanDetail();
    detail3.setExcManName("3");
    PlanDetail detail4 = new PlanDetail();
    detail4.setExcManName("2");
    mtaskList.add(detail2);
    mtaskList.add(detail3);
    mtaskList.add(detail4);
    List<ExcManContent> list = getExcManContents(mtaskList);

    assertEquals(2, list.size());
  }

  @NonNull private List<ExcManContent> getExcManContents(ArrayList<PlanDetail> mtaskList) {
    //ArrayList<PlanDetail> listWithoutDup = new ArrayList<PlanDetail>(new HashSet<PlanDetail>(mtaskList));

    Collections.sort(mtaskList, new PlanDetail().new MyComparator());
    //属性去重,重写了equals和hashCode的方法.
    //mtaskList=new ArrayList<>(new HashSet<>(mtaskList));

    //// 将执行人工号和姓名添加到集合list
    Log.i("mList", mtaskList.size() + "  mList");
    List<ExcManContent> list = new ArrayList<>();
    for (int i = 0; i < mtaskList.size(); i++) {
      if (list.size() == 0) {

        list.add(
            new ExcManContent(mtaskList.get(i).getExcManId(), mtaskList.get(i).getExcManName()));
      }

      if (!(mtaskList.get(i).getExcManName()).equals(list.get(list.size() - 1).getExcManName())) {
        ExcManContent content =
            new ExcManContent(mtaskList.get(i).getExcManId(), mtaskList.get(i).getExcManName());
        list.add(content);
      }
    }
    return list;
  }

  /**
   * 测试gradle配置
   *
   * @throws Exception
   */
  //@Test public void testConfig() throws Exception {
  //  //LogUtil.i("config="+ BuildConfig.);
  //  //assertEquals(true,BuildConfig.FLAVOR.equals(MyConst.FLAVOR_ZHIBAOYUN));
  //  assertEquals(false, BuildConfig.FLAVOR.equals(MyConst.FLAVOR_GUOXIN));
  //  assertEquals(false, BuildConfig.FLAVOR.equals(MyConst.FLAVOR_WEIJIA));
  //}

  /**
   * 测试logger
   *
   * @throws Exception
   */
  @Test public void testLog() throws Exception {
    LogUtil.i("url=", "test url");
  }

  @Test public void testSplitStr() throws Exception {
    assertEquals(MyDateUtil.parse("2015-13-14 22:22:11") != null, true);
    assertEquals(MyDateUtil.parse("2015-13-14 22:22") != null, true);
    assertEquals(MyDateUtil.parse("2015-13-14 22") != null, true);
    assertEquals(MyDateUtil.parse("2015-13-14 ") != null, true);
    assertEquals(MyDateUtil.parse("2015-13-14") != null, true);
    assertEquals(MyDateUtil.parse("2015-13") != null, true);
    assertEquals(MyDateUtil.format(new Date()) != null, true);
    assertEquals(MyDateUtil.format(new Date(), MyDateUtil.TYPE_HOUR) != null, true);
  }

  @Test public void testGetDown() throws Exception {
    String path =
        "100014_北京信普达系统工程有限公司/app/plot_mapping_information/ad6d3596-49e4-4a61-94b3-9b843d74de8f/PANO_20170311_170947.jpg";
    String url = ApiConst.getDownLoadUrlByGet(path);
    LogUtil.i("getDown=", url);
    //assertEquals(MyDateUtil.format(new Date()) != null, true);
  }

  //@Test public void testPattern() throws Exception {
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("ab").matches(), true);
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("abcdef").matches(), true);
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("Abc").matches(), true);
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("12").matches(), false);
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("w我").matches(), false);
  //  Assert.assertEquals(!Pattern.compile("[a-zA-Z]{2,6}").matcher("").matches(), false);
  //}

  //@Test public void testDate() throws Exception {
  //  //本周结束
  //  Assert.assertEquals(MyDateUtil.format(MyDateUtil.getBeginWeekByDate(new Date())),
  //      "2017-05-15 00:00:00");
  //  Assert.assertEquals(MyDateUtil.format(MyDateUtil.getEndWeekByDate(new Date())),
  //      "2017-05-21 23:59:59");
  //  //本周开始
  //  Assert.assertEquals(
  //      MyDateUtil.format(MyDateUtil.getBeginWeekByDate(MyDateUtil.parse("2017-05-15 16:52:00"))),
  //      "2017-05-15 00:00:00");
  //  Assert.assertEquals(
  //      MyDateUtil.format(MyDateUtil.getEndWeekByDate(MyDateUtil.parse("2017-05-15 16:52:00"))),
  //      "2017-05-21 23:59:59");
  //  //中间
  //  Assert.assertEquals(
  //      MyDateUtil.format(MyDateUtil.getBeginWeekByDate(MyDateUtil.parse("2017-05-18"))),
  //      "2017-05-15 00:00:00");
  //  Assert.assertEquals(
  //      MyDateUtil.format(MyDateUtil.getEndWeekByDate(MyDateUtil.parse("2017-05-18"))),
  //      "2017-05-21 23:59:59");
  //}

  //@Test public void testConfigByDefault() throws Exception {
  //  SharedPreferencesUtil.setBoolean(mActivity.getActivity(), SharedPreferencesUtil.KEY_IS_BUGTAGS,
  //      false);
  //  Assert.assertEquals(true, MyUtils.isBugTags(mActivity.getActivity()));
  //
  //  SharedPreferencesUtil.setBoolean(mActivity.getActivity(), SharedPreferencesUtil.KEY_IS_BUGTAGS,
  //      true);
  //  Assert.assertEquals(true, MyUtils.isBugTags(mActivity.getActivity()));
  //}

  /** 测试Java Object,json字符串,hashmap,postman字符串互转 */
  @Test public void testConvert() throws Exception {
    //JavaObject转json字符串.
    BaseParams obj = new BaseParams();
    obj.setRemark("111");
    //转 json字符串
    String json = new Gson().toJson(obj);
    LogUtil.i("json=", json);
    //转HashMap
    HashMap<String, String> map = MyUtils.JsontoHashMap(json);
    LogUtil.i("hashmap=", map.toString());
    //转postman
    String postMan = MyStringUtil.paramsToPostman(map);
    LogUtil.i("postman=", postMan);
  }

  @Test public void testStringUpperCase() throws Exception {
    Assert.assertEquals("SLA", MyStringUtil.getAllUpperCase("SelectLaunchActivity"));
    String name = "SelectLaunchActivity";
    String newText = "sla";
    Assert.assertEquals(true,
        name.toLowerCase().contains(newText.toLowerCase()) || MyStringUtil.getAllUpperCase(name)
            .toLowerCase()
            .contains(newText.toLowerCase()));
  }

  @Test public void gsonTest() throws Exception {
    String json = "{\n"
        + "    \"message\":\"获取企业证章使用申请列表成功！ \",\"result\":1,\"rows\":[\n"
        + "    {\n"
        + "        \"actBackDays\":\"\",\"actUseDays\":\"\",\"agent\":\"\",\"agentCode\":\"\",\"applyNo\":\"5430976128\",\"applyText\":\"\",\"applydeptCode\":\"100014003\",\"applydeptDate\":\"2017-05-24 01:24:00\",\"applydeptName\":\"产品二部\",\"confirmCode\":\"\",\"confirmName\":\"\",\"confirmTime\":\"\",\"createBy\":\"\",\"createtime\":\"\",\"dataStatus\":\"waitCheck\",\"defId\":10000044200447,\"docBlob\":\"\",\"docPath\":\"\",\"id\":\"6F8137BAF4A34D3583660C7EB7542C53_30682\",\"isCarryOut\":\"\",\"isCarryOutName\":\"\",\"memberCode\":\"100014\",\"orderNo\":158,\"regDate\":\"2017-05-24\",\"regStaffId\":\"1000140201\",\"regStaffName\":\"刘卫平\",\"remark\":\"\",\"runId\":0,\"sealsType\":\"\",\"sealsTypeCode\":\"\",\"sealsTypeName\":\"\",\"updateBy\":\"\",\"updatetime\":\"\"\n"
        + "    }\n"
        + "    ,\n"
        + "    {\n"
        + "        \"actBackDays\":\"\",\"actUseDays\":\"\",\"agent\":\"\",\"agentCode\":\"\",\"applyNo\":\"2496175830\",\"applyText\":\"\",\"applydeptCode\":\"100014003\",\"applydeptDate\":\"2017-05-24 01:13:00\",\"applydeptName\":\"产品二部\",\"confirmCode\":\"\",\"confirmName\":\"\",\"confirmTime\":\"\",\"createBy\":\"\",\"createtime\":\"\",\"dataStatus\":\"new\",\"defId\":10000044200447,\"docBlob\":\"\",\"docPath\":\"\",\"id\":\"4044C12CD5204EAEA2DA70E30F6A4CA4_69103\",\"isCarryOut\":\"\",\"isCarryOutName\":\"\",\"memberCode\":\"100014\",\"orderNo\":155,\"regDate\":\"2017-05-24\",\"regStaffId\":\"1000140201\",\"regStaffName\":\"刘卫平\",\"remark\":\"\",\"runId\":0,\"sealsType\":\"\",\"sealsTypeCode\":\"\",\"sealsTypeName\":\"\",\"updateBy\":\"\",\"updatetime\":\"\"\n"
        + "    }\n"
        + "    ,\n"
        + "    {\n"
        + "        \"actBackDays\":\"\",\"actUseDays\":\"\",\"agent\":\"\",\"agentCode\":\"\",\"applyNo\":\"6209418375\",\"applyText\":\"2121\",\"applydeptCode\":\"100014003\",\"applydeptDate\":\"2017-05-17 16:03:00\",\"applydeptName\":\"产品二部\",\"confirmCode\":\"\",\"confirmName\":\"\",\"confirmTime\":\"\",\"createBy\":\"\",\"createtime\":\"\",\"dataStatus\":\"new\",\"defId\":10000044200447,\"docBlob\":\"\",\"docPath\":\"\",\"id\":\"24B4C3D62B7D4846BAD590583762E38A_25064\",\"isCarryOut\":\"\",\"isCarryOutName\":\"\",\"memberCode\":\"100014\",\"orderNo\":154,\"regDate\":\"2017-05-17\",\"regStaffId\":\"1000140201\",\"regStaffName\":\"刘卫平\",\"remark\":\"\",\"runId\":0,\"sealsType\":\"公章,合同专用章\",\"sealsTypeCode\":\"01,02\",\"sealsTypeName\":\"\",\"updateBy\":\"\",\"updatetime\":\"\"\n"
        + "    }\n"
        + "    ,\n"
        + "    {\n"
        + "        \"actBackDays\":\"\",\"actUseDays\":\"\",\"agent\":\"\",\"agentCode\":\"\",\"applyNo\":\"\",\"applyText\":\"fdfdsafdsaf\",\"applydeptCode\":\"100014003\",\"applydeptDate\":\"2017-05-12 09:56:00\",\"applydeptName\":\"产品二部\",\"confirmCode\":\"\",\"confirmName\":\"\",\"confirmTime\":\"\",\"createBy\":\"\",\"createtime\":\"\",\"dataStatus\":\"new\",\"defId\":10000044200447,\"docBlob\":\"\",\"docPath\":\"\",\"id\":\"5B9B3B6B1835480FB6463FC8095A3991_15298\",\"isCarryOut\":\"\",\"isCarryOutName\":\"\",\"memberCode\":\"100014\",\"orderNo\":153,\"regDate\":\"2017-05-12\",\"regStaffId\":\"1000140201\",\"regStaffName\":\"刘卫平\",\"remark\":\"\",\"runId\":0,\"sealsType\":\"公章\",\"sealsTypeCode\":\"01\",\"sealsTypeName\":\"\",\"updateBy\":\"\",\"updatetime\":\"\"\n"
        + "    }\n"
        + "    ,\n"
        + "    {\n"
        + "        \"actBackDays\":\"\",\"actUseDays\":\"2015-10-14 23:49:22\",\"agent\":\"4444\",\"agentCode\":\"\",\"applyNo\":\"\",\"applyText\":\"656565\",\"applydeptCode\":\"100014003\",\"applydeptDate\":\"2015-10-14 23:35:00\",\"applydeptName\":\"产品二部\",\"confirmCode\":\"\",\"confirmName\":\"\",\"confirmTime\":\"\",\"createBy\":\"\",\"createtime\":\"\",\"dataStatus\":\"checkPass\",\"defId\":10000044200447,\"docBlob\":\"\",\"docPath\":\"\",\"id\":\"95E0F7E3A94243FCB7E424F5C20E5834_73520\",\"isCarryOut\":\"\",\"isCarryOutName\":\"\",\"memberCode\":\"100014\",\"orderNo\":6,\"regDate\":\"2015-10-14\",\"regStaffId\":\"1000140201\",\"regStaffName\":\"刘卫平\",\"remark\":\"\",\"runId\":10000044220396,\"sealsType\":\"公章\",\"sealsTypeCode\":\"01\",\"sealsTypeName\":\"\",\"updateBy\":\"\",\"updatetime\":\"\"\n"
        + "    }\n"
        + "    ],\"total\":5\n"
        + "}";

    //不再重复定义Result类
    Type userType = new TypeToken<BaseOriginal<RSealsUseApp>>() {
    }.getType();
    BaseOriginal<RSealsUseApp> result = new Gson().fromJson(json, userType);
    Assert.assertEquals(5, result.getTotal());
    Assert.assertEquals(5, result.getRows().size());
    Assert.assertEquals("5430976128", result.getRows().get(0).getApplyNo());
    Assert.assertEquals(MyDateUtil.parse("2017-05-24 01:24:00"),
        result.getRows().get(0).getApplydeptDate());
    Assert.assertEquals(MyDateUtil.parse(""), result.getRows().get(0).getActUseDays());

    RSealsUseApp item = new RSealsUseApp();
    item.setApplydeptDate(MyDateUtil.parse("2017-05-24 01:24:00"));
    json = new Gson().toJson(item);
    Assert.assertEquals(true, json.contains("2017-05-24 01:24:00"));
  }

  @Test public void testLogger() throws Exception {
    HashMap<String, String> params =
        new ParamsNotEmpty().put("abc", "123").put("key2", "value2").getParams();
    LogUtil.i("url=", ApiConst.getAbsoluteUrl(ApiConst.URL_LATEST_VERSION));
    LogUtil.d("params=", params);
    LogUtil.i("postman=", MyStringUtil.paramsToPostman(params));
    LogUtil.i("paramsJson=", MyStringUtil.toJson(params));

    SharedPreferencesUtil.setBoolean(mActivity.getActivity(), SharedPreferencesUtil.KEY_IS_LOG,
        false);

    LogUtil.i("url=", ApiConst.getAbsoluteUrl(ApiConst.URL_LATEST_VERSION));
    LogUtil.d("params=", params);
    LogUtil.i("postman=", MyStringUtil.paramsToPostman(params));
    LogUtil.i("paramsJson=", MyStringUtil.toJson(params));

    SharedPreferencesUtil.setBoolean(mActivity.getActivity(), SharedPreferencesUtil.KEY_IS_LOG,
        true);
  }
}